<?php
/* SVN FILE: $Id$ */
/**
 * theme-options.php   The functions file for the theme options page
 *  
 * This file contains all functions relevant to the creation, display and usage
 * of the theme options for the CP Construction Wordpress Theme
 *
 *  @package        CPConstruction
 *  @subpackage     Functions
 *  @copyright      CP Construction (Gwent) Ltd <http://www.cpconstruction.co.uk/> 2012
 *  @author         Richard Perry <http: //www.perry-online.me.uk/>
 *  @since          Release 2.0.0
 *  @version        $Rev$
 *  @modifiedby     $LastChangedBy$
 *  @lastmodified   $Date: 2012-09-20 11:08:36 +0100 (Thu, 20 Sep 2012) $
 *
 *  @todo           ToDo List
 *                  -
 */


add_action( 'admin_init', 'theme_options_init' );
add_action( 'admin_menu', 'theme_options_add_page' );

/**
 * Init plugin options to white list our options
 */
function theme_options_init(){
    register_setting( 'cpcons_options', 'cpcons_theme_options', 'theme_options_validate' );
}

/**
 * Load up the menu page
 */
function theme_options_add_page() {
    add_theme_page( 
        __( 'Theme Options', 'cpcons' ), 
        __( 'Theme Options', 'cpcons' ), 
        'edit_theme_options', 
        'theme_options', 
        'theme_options_do_page' 
    );
}


/**
 * Create the options page
 */
function theme_options_do_page() {
    if ( ! isset( $_REQUEST['settings-updated'] ) )
        $_REQUEST['settings-updated'] = false;

    ?>
    <div class="wrap">
        <?php screen_icon(); echo "<h2>" . get_current_theme() . __( ' Theme Options', 'cpcons' ) . "</h2>"; ?>

        <?php if ( false !== $_REQUEST['settings-updated'] ) : ?>
        <div class="updated fade"><p><strong><?php _e( 'Options saved', 'cpcons' ); ?></strong></p></div>
        <?php endif; ?>

        <form method="post" action="options.php">
            <?php settings_fields( 'cpcons_options' ); ?>
            <?php $options = get_option( 'cpcons_theme_options' ); ?>
            
            <h3>Footer Content</h3>
            <table class="form-table">

                <?php
                /**
                 * Copyright Details for display
                 */
                ?>
                <tr valign="top">
                    <th scope="row"><?php _e( 'Company Name', 'cpcons' ); ?></th>
                    <td>
                        <input id="cpcons_theme_options[copyright]" 
                            class="regular-text" type="text" 
                            name="cpcons_theme_options[copyright]" 
                            value="<?php esc_attr_e( $options['copyright'] ); ?>" 
                        />
                        <label class="description hidden" 
                            for="cpcons_theme_options[copyright]">
                            Copyright Info
                        </label>
                    </td>
                </tr>
                
                <?php
                /**
                 * Company Registration for display
                 */
                ?>
                <tr>
                    <th scope="row"><?php _e( 'Company Registration', 'cpcons' ); ?></th>
                    <td>
                        <input id="cpcons_theme_options[registration]" 
                            class="regular-text" type="text" 
                            name="cpcons_theme_options[registration]" 
                            value="<?php esc_attr_e( $options['registration'] ); ?>" 
                        />
                        <label class="description hidden" 
                            for="cpcons_theme_options[registration]">
                            Company Registration
                        </label>
                    </td>
                </tr>

                <?php
                /**
                 * What text to include in the company info section of the footer
                 */
                ?>
                <tr valign="top"><th scope="row"><?php _e( 'Company Info', 'cpcons' ); ?></th>
                    <td>
                        <textarea id="cpcons_theme_options[company-info]" 
                            class="large-text" cols="50" rows="10" 
                            name="cpcons_theme_options[company-info]"><?php 
                            echo esc_textarea( $options['company-info'] ); 
                        ?></textarea>
                        <label class="description hidden" 
                            for="cpcons_theme_options[company-info]">
                            Company Info
                        </label>
                    </td>
                </tr>
            </table>

            <p class="submit">
                <input type="submit" class="button-primary" value="<?php _e( 'Save Options', 'cpcons' ); ?>" />
            </p>
        </form>
    </div>
    <?php
}

/**
 * Sanitize and validate input. Accepts an array, return a sanitized array.
 */
function theme_options_validate( $input ) {

    // Say our text option must be safe text with no HTML tags
    $input['copyright'] = wp_filter_nohtml_kses( $input['copyright'] );

    // Say our textarea option must be safe text with the allowed tags for posts
    $input['company-info'] = wp_filter_post_kses( $input['company-info'] );

    return $input;
}

// adapted from http://planetozh.com/blog/2009/05/handling-plugins-options-in-wordpress-28-with-register_setting/